<?php
namespace \DragonU\DB;
/**
 * DragonU DB library.
 *
 * @internal
 * Copyright (c) 2009, Jacob Santos
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the DragonU nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @author Jacob Santos <wordpress@santosj.name>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
 * @package DragonU
 * @subpackage DB
 * @since 0.1
 */

/**
 * CorePress DB Cache class.
 *
 * Stores all of the table names that are used with the prefix. This will take
 * the table and add the prefix to it. Therefore, those using the class can use
 * the default tables with the set prefix without neededing to know the given
 * prefix or add their own using the class.
 *
 * @author Jacob Santos
 * @since 0.1
 * @package DragonU
 * @subpackage DB
 */
class Cache
{
	/**
	 * Query type of save.
	 *
	 * @since 0.1
	 * @var int
	 */
	const QUERY_TYPE = 1;

	/**
	 * Column type of save.
	 *
	 * @since 0.1
	 * @var int
	 */
	const COLUMN_TYPE = 2;

	/**
	 * The implementation class for caching.
	 *
	 * @since 0.1
	 * @var \CorePress\DB\Caching\Implementation
	 */
	protected $implementation = null;

	/**
	 * Whether caching is enabled.
	 *
	 * @since 0.1
	 * @var bool
	 */
	protected $caching = false;

	/**
	 * Setup the default caching implementation.
	 *
	 * @since 0.1
	 */
	public function __construct()
	{

	}

	/**
	 * Change caching implementation.
	 *
	 * @since 0.1
	 *
	 * @param \CorePress\DB\Caching\Implementation $cache
	 */
	public function implementation( Caching\Implementation $cache )
	{
		$this->implementation = $cache;
	}

	/**
	 * Whether to enable or disable caching.
	 *
	 * @since 0.1
	 *
	 * @param bool $caching
	 * @return bool Caching state before change.
	 */
	public function caching($enabled = null)
	{
		$previous = $this->caching;

		if( is_bool( $enabled ) )
		{
			$this->caching = $enabled;
		}

		return $previous;
	}

	/**
	 * Cache information about either query or column.
	 *
	 * @since 0.1
	 *
	 * @param string $query
	 * @param mixed $results
	 * @param int $type Either QUERY_TYPE or COLUMN_TYPE class constants.
	 */
	public function save($query, $results, $type = self::QUERY_TYPE)
	{

	}

	public function last($type = self::QUERY_TYPE)
	{
		if( $this->implementation instanceof Caching\Implementation )
		{
			return $this->implementation->query( $type );
		}

		return false;
	}

	public function flush()
	{
		if( $this->implementation instanceof Caching\Implementation )
		{
			return $this->implementation->flush();
		}
	}

	public function hits()
	{
		if( $this->implementation instanceof Caching\Implementation )
		{
			return $this->implementation->hits;
		}

		return 0;
	}

	public function misses()
	{
		if( $this->implementation instanceof Caching\Implementation )
		{
			return $this->implementation->misses;
		}

		return 0;
	}
}